<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-04 15:22:10
 * @LastEditTime: 2023-11-12 13:52:22
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\SoftDeletesTrait;
use Illuminate\Support\Facades\Storage;

class Employee extends BaseModel
{
    use HasFactory, AttachmentTrait, SoftDeletesTrait, AutoCodeTrait;
    public $table = "basedata_employee";
    protected $ACSign = 'EMPLOYEE_CODE';
    protected $guarded = [];
    protected $appends = ['avatar', 'thumb_avatar'];
    public function getAvatarAttribute()
    {
        return $this->firstAttachment == null ? '#' : Storage::url($this->firstAttachment?->filepath);
    }
    public function getThumbAvatarAttribute()
    {
        return $this->firstAttachment == null ? '#' : Storage::url($this->firstAttachment->filethumbnail);
    }

    public function department()
    {
        return $this->belongsTo(Department::class);
    }

    public function education()
    {
        return $this->belongsTo(Dic::class, "education_id", "key")->where(function ($q) {
            $q->where("code", 'Education');
        });
    }
    public function contactor_relation()
    {
        return $this->belongsTo(Dic::class, "contactor_relation_id", "key")->where(function ($q) {
            $q->where("code", 'Relation');
        });
    }
    public function marriage()
    {
        return $this->belongsTo(Dic::class, "marriage_id", "key")->where(function ($q) {
            $q->where("code", 'Marriage');
        });
    }
    public function shift()
    {
        return $this->belongsTo(Shift::class);
    }
    public function position()
    {
        return $this->belongsTo(Position::class);
    }
    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
